package com.wjy.Data_Structure.linearlist.arraylistimpl;

import com.wjy.Data_Structure.linearlist.common.List;
import com.wjy.Data_Structure.linearlist.exception.OutOfBoundaryException;
import com.wjy.Data_Structure.strategy.DefaultStrategy;
import com.wjy.Data_Structure.strategy.Strategy;

//线性表的数组实现
public class ListArray implements List {

	private final int LEN = 8;// 数组的默认大小
	private Strategy strategy; // 数据元素比较策略
	private int size;// 线性表中数据元素的个数
	private Object[] elements; // 数据元素数组

	public ListArray() {
		this(new DefaultStrategy());
	}

	public ListArray(Strategy strategy) {
		this.strategy = strategy;
		size = 0;
		elements = new Object[LEN];
	}

	@Override
	public int getSize() {
		return size;
	}

	@Override
	public boolean isEmpty() {
		return size == 0;
	}

	@Override
	public boolean contains(Object e) {
		return indexOf(e) != -1;

	}

	@Override
	public int indexOf(Object e) {
		for (int i = 0; i < size; i++)
			if (strategy.equal(e, elements[i]))
				return i;
		return -1;
		// int num = -1;
		// for (int i = 0; i < size; i++) {
		// if (strategy.equal(e, elements[i])) {
		// num = i;
		// break;
		// }
		// }
		// return num;
	}

	/**
	 * 扩充数组
	 */
	private void expandSpace() {
		Object[] a = new Object[elements.length * 2];
		for (int i = 0; i < elements.length; i++)
			a[i] = elements[i];
		elements = a;
	}

	@Override
	public void insert(int i, Object e) throws OutOfBoundaryException {
		if (i < 0 || i > size)
			throw new OutOfBoundaryException("错误，指定的插入序号越界");
		if (size >= elements.length)
			expandSpace();
		for (int j = size; j > i; j--)
			elements[j] = elements[j - 1];
		elements[i] = e;
		size++;
		return;
	}

	@Override
	public boolean insertBefore(Object obj, Object e) {
		int i = indexOf(obj);
		if (i < 0)
			return false;
		insert(i, e);
		return true;
	}

	@Override
	public boolean insertAfter(Object obj, Object e) {
		int i = indexOf(obj);
		if (i < 0)
			return false;
		insert(i + 1, e);
		return true;
	}

	@Override
	public Object remove(int i) throws OutOfBoundaryException {
		if (i < 0 || i >= size)
			throw new OutOfBoundaryException("错误，指定的插入序号越界");
		Object obj = elements[i];
		for (int j = i; j < size - 1; j++)
			elements[j] = elements[j + 1];
		elements[--size] = null;
		return obj;
	}

	@Override
	public boolean remove(Object e) throws OutOfBoundaryException {
		int i = indexOf(e);
		if (i < 0)
			return false;
		remove(i);
		return true;
	}

	@Override
	public Object replace(int i, Object e) throws OutOfBoundaryException {
		if (i < 0 || i >= size)
			throw new OutOfBoundaryException("错误，指定的序号越界");
		Object obj = elements[i];
		elements[i] = e;
		return obj;
	}

	@Override
	public Object get(int i) throws OutOfBoundaryException {
		if (i < 0 || i >= size)
			throw new OutOfBoundaryException("错误，指定的序号越界");
		return elements[i];
	}

}
